package myComponent

import (
	"fmt"
	"log"
	"time"
)

var SessionContainer = map[string]MessageList{}

// 用于session存储的结构体
type MessageList struct {
	Messages   []Message `json:"messages"`
	SessionId  string    `json:"sessionId"`
	ExpireTime time.Time `json:"expireTime"`
}

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

func GenerateSessionId() string {
	return fmt.Sprintf("%d", time.Now().UnixNano())
}

func init() {
	// 定时删除过期的会话缓存
	go func() {
		for {
			time.Sleep(time.Second * 10) // 休眠10秒
			for k, v := range SessionContainer {
				if v.ExpireTime.Before(time.Now()) {
					log.Printf("删除过期的对话缓存:%s", k)
					delete(SessionContainer, k)
				}
			}
		}
	}()
}
